const app = getApp()
Page({
	data : {
		conversations : [],
		action : {
			conversation : null,
			show : false,
			toastMessage : '',
			showToast : false
		}
	},
	onShow () {
		let service = app.globalData.imService;

		if(!service.currentUser){
			wx.redirectTo({
				url : '../login/login'
			})
		}

		wx.showLoading({
			title: '加载中'
		});

		//监听会话列表变化
		let self = this;
		wx.im.on(wx.GoEasyIM.EVENT.CONVERSATIONS_UPDATED, (conversations) => {
			// 设置tabBar未读消息总数以及conversation
			self.setConversations(conversations);
		});

		//加载会话列表
		wx.im.latestConversations()
			.then(res => {
				let content = res.content;
				self.setConversations(content);
				wx.hideLoading();
			})
			.catch(e => {
				console.log(e);
				wx.hideLoading();
			});
	},
	onHide(){
		// 销毁conversation监听器
		wx.im.on(wx.GoEasyIM.EVENT.CONVERSATIONS_UPDATED, (conversations) => {});
	},
	setConversations (conversations) {
		conversations.conversations && conversations.conversations.map((item) => {
			// 重新定义一个变量方便发送时间展示在页面
			item.lastMessage.date = app.formatDate(item.lastMessage.timestamp)
		});
		this.setData({
			conversations : conversations.conversations
		});
		this.setUnreadAmount(conversations.unreadTotal);
	},
	navigateToChat (e) {
		let conversation = e.currentTarget.dataset.conversation;
		let path = conversation.type === wx.GoEasyIM.SCENE.PRIVATE?
			       '../chat/privateChat/privateChat?to='+conversation.userId
					:'../chat/groupChat/groupChat?to='+ conversation.groupId;
		wx.navigateTo({
			url : path
		});
	},
	setUnreadAmount(unreadTotal) {
		if(unreadTotal >0){
			wx.setTabBarBadge({
				index: 0,
				text: unreadTotal.toString()
			});
		}else{
			wx.hideTabBarRedDot({
				index :0
			});
		}
	},
	showAction(e){
		let conversation = e.currentTarget.dataset.conversation;
		this.setData({
			["action.conversation"]: conversation,
			["action.show"]: true
		});
	},
	topConversation(){
		let conversation = this.data.action.conversation;
		let title = conversation.top ? '取消置顶失败' : '置顶失败';
		let promise;
		wx.showLoading({
			title: ""
		});
		if(conversation.type === wx.GoEasyIM.SCENE.PRIVATE){
			promise = wx.im.topPrivateConversation(conversation.userId, !conversation.top)
		}else{
			promise = wx.im.topGroupConversation(conversation.groupId, !conversation.top)
		}
		this.afterDoAction(promise, title)
	},
	removeConversation(){
		wx.showLoading(null);
		let conversation = this.data.action.conversation;
		let promise;
		if(conversation.type === wx.GoEasyIM.SCENE.PRIVATE){
			promise = wx.im.removePrivateConversation(conversation.userId);
		}else{
			promise = wx.im.removeGroupConversation(conversation.groupId);
		}
		this.afterDoAction(promise, '删除中')
	},
	afterDoAction (promise, title) {
		promise.then(() => {
			wx.hideLoading()
		}).catch(() => {
			let self = this;
			wx.hideLoading();
			this.setData({
				["action.showToast"]: true,
				["action.toastMessage"]: title,
			});
			setTimeout(() => {
				self.setData({
					["action.showToast"]: false
				});
			},2000);
		});
		this.setData({
			["action.show"]: false
		})
	},
	closeMask(){
		// 关闭弹窗
		this.setData({
			["action.show"]: false
		})
	},
})